import { reqGetGoodsInfo, reqAddOrUpdateShopCart } from '@/api/request'
// 引入生成随机字符串的封装好的函数
import { getUUID } from '@/utils/uuid_token'
const state = {
    goodInfo:{},
    uuid_token:getUUID(),
}
const mutations = {
    GETGOODINFO(state,goodInfo){
        state.goodInfo = goodInfo
    },
}
const actions = {
    // 获取商品信息
    async getgoodInfo({commit},skuId){
       let result = await reqGetGoodsInfo(skuId)
       if(result.code==200){
           commit('GETGOODINFO',result.data)
       }
    },
    // 将产品添加到购物车中 ,此时不需要三连环存储数据，
    async AddOrUpdateShopCart({commit},{skuId,skuNum}){
        let result = await reqAddOrUpdateShopCart(skuId,skuNum)
        if(result.code===200){
            return '添加成功'
        }else{
            return Promise.reject(new Error('加入购物车失败'))
        }
    },
    

}
const getters = {
    categoryView(state){
        return state.goodInfo.categoryView || {}
    },
    skuInfo(state){
        return state.goodInfo.skuInfo || {}
    },
    spuSaleAttrList(state){
        return state.goodInfo.spuSaleAttrList || []
    },
}

export default{
    state,
    mutations,
    actions,
    getters,
}